package dice.sinanya.tools.makedata;

import dice.sinanya.entity.EntityReply;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static dice.sinanya.system.MessagesSystem.loginInfo;

public class XmlFromReply {
    HashMap<String, ArrayList<EntityReply>> replyHash;
    String xmlString = "";

    public XmlFromReply(HashMap<String, ArrayList<EntityReply>> replyHash) {
        this.replyHash = replyHash;
    }

    public String getXml() {
        return build();
    }

    private String build() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.newDocument();
            document.setXmlStandalone(true);

            Element reply = document.createElement("reply");
            document.appendChild(reply);

            for (Map.Entry<String, ArrayList<EntityReply>> replyEntry : replyHash.entrySet()) {
                if (replyEntry.getKey().equals(String.valueOf(loginInfo.getLoginId()))||replyEntry.getKey().equals(String.format("[CQ:at,qq=%s,display=@%s]",loginInfo.getLoginId(),loginInfo.getLoginName()))){
                    continue;
                }
                if (replyEntry.getKey().length() < 4 || replyEntry.getKey().equals(loginInfo.getLoginName())) {
                    if (replyEntry.getKey().equals(loginInfo.getLoginName())) {
                        Element group = document.createElement("自身");
                        String me = replyHash.getOrDefault("自身", replyEntry.getValue()).get(0).getValue();
                        group.setTextContent(me);
                        reply.appendChild(group);
                    }
                    continue;
                }
                Element group = document.createElement("组");
                group.setAttribute("关键字", replyEntry.getKey());
                for (EntityReply entityReply : replyEntry.getValue()) {
                    Element value = document.createElement("回复");
                    value.setTextContent(entityReply.getValue());
                    Element weight = document.createElement("权重");
                    weight.setTextContent(String.valueOf(entityReply.getWeight()));

                    Element valueGroup = document.createElement("组回复");
                    if (entityReply.isPublicMode()){
                        valueGroup.setAttribute("公共","是");
                    }
                    valueGroup.appendChild(value);
                    valueGroup.appendChild(weight);
                    group.appendChild(valueGroup);
                }
                reply.appendChild(group);
            }

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            Source xmlSource = new DOMSource(document);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Result transformResult = new StreamResult(baos);
            transformer.transform(xmlSource, transformResult);

            xmlString = baos.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return xmlString;
    }
}
